<?php
/**
 * @file checkurl.php 检查81OSCAR.INFO的推广网址和翻译网址重复性
 * @package Trackback
 * @subpackage 
 * @version $id: 0.1 UTF-8 2009-2-26 下午02:46:33 $
 * @author LD King (mifunny.info)
 * @copyright Copyright (c) 2007 - 2009 MiFunny China Inc. (http://mifunny.info)
 */
$submit = $_POST['submit'];
$check_url = trim($_POST['check_url']);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="zh-CN">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Check URL | 水草树</title>
<meta name="robots" content="nofollow" />
<style type="text/css">
#wrapper{ width:800px;margin:0 auto;text-align:center;}
#header{height:18px;text-align:right;font-size:12px;position:relative;}
#content{font-size:14px;}
#footer {margin:75px 0 25px 0;position:relative;}
.box{border:2px solid #333; background-color:#ffffce;margin:10px;padding:5px;}
.error{color:red;}
.success{color:blue;}
.old{color:gray;}
</style>
</head>
<body>
<div id="wrapper">
	<div id="header">
		<p><a href="checkurl.php">CHECK URL</a> | <a href="trackback.php">SEND TRACKBACK</a></p>
	</div>

<div id="content">
<p>输入你要查询的URL<br />如果重复则提示，单一则入库！</p>
<form method="post" action="checkurl.php">
      Check URL: <input type="text" name="check_url" size="100" />
      <br /><input type="submit" name="submit" />
</form>
<?php 
if (!empty($submit)){
	try {
		$dbh = new PDO("sqlite:SQLTrackback.db", null, null, array(PDO::ATTR_PERSISTENT => true));
	}catch (PDOException $ex){
		die("[Error]: ".$ex->getMessage().'<br />\n');
	}
	// Useless but carefully is better, set encoding.
	// Sqlite always use utf-8 internaly, UTF-8 is also it's default value.
	$dbh->query('PRAGMA encoding = "UTF-8"');
	$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	// 从OSCAR表里查询
	$sqlSelect = "SELECT id, url FROM oc WHERE url = \"".$check_url."\" ;";
	
	$result = $dbh->query($sqlSelect);
	$row = $result->fetch(PDO::FETCH_BOTH);
	
	if (!empty($row[0])){ //已含有URL
		?>
<div class="box">The URL had been <span class="old">submit</span>!<br />URL: <span class="old"><?php echo $check_url; ?></span></div>	
		<?php 
	}else {
		$sqlInsert = "INSERT INTO oc VALUES(null, \"".$check_url."\" , julianday('now') )";
		if ($dbh->exec($sqlInsert) != 1){
			?>
<div class="box">URL submit <span class="error">Error</span>!<br />URL: <span class="error"><?php echo $check_url; ?></span></div>
			<?php 
		}else {
			?>
<div class="box">URL submit <span class="success">Successfully</span>!<br />URL: <span class="success"><?php echo $check_url; ?></span></div>
			<?php
		}
	}//END 判断URL重复	
}// END if
?>
<div><!-- #content -->
<div id="footer">
	<p>Copyright &copy; 2008 - 2009, <a href="http://mifunny.info/">水草树</a>, All right reserved.</p>
</div>

</div><!-- #wrapper -->
</body>
</html>